﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using HRM.DAL.DAO;

namespace HRM.DAL.Gateway
{
    public class CatagoryGateway : DBGateway
    {
        public bool HasThisCatagoryAlreadyExist(Catagory catagory)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = @"select EmpCatagory from tblCatagories where EmpCatagory='" + catagory.EmpCatagory + "'";
                SqlDataReader reader = CommandObj.ExecuteReader();
                while (reader.Read())
                {
                    return true;
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                ConnectionObj.Close();
            }
            return false;
        }
        public string Save(Catagory catagory)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = string.Format("insert into tblCatagories Values(@EmpCatagory)");
                CommandObj.Parameters.Clear();
                CommandObj.Parameters.Add("@EmpCatagory", SqlDbType.VarChar).Value = catagory.EmpCatagory;
                CommandObj.ExecuteNonQuery();
                return "Catagory Saved Successfully";
            }
            catch (Exception)
            {
                throw new Exception("Error Ocured during Saving Catagory, Please Try again !");
            }
            finally
            {
                ConnectionObj.Close();
            }

        }

        public List<Catagory> GetAllCatagories()
        {
            try
            {
                List<Catagory> catagories = new List<Catagory>();
                ConnectionObj.Open();
                CommandObj.CommandText = "Select * from tblCatagories order by EmpCatagory";
                SqlDataReader reader = CommandObj.ExecuteReader();
                while (reader.Read())
                {
                    Catagory catagory = new Catagory();
                    catagory.Id = Convert.ToInt32(reader[0]);
                    catagory.EmpCatagory = reader[1].ToString();
                    catagories.Add(catagory);
                }
                return catagories;
            }
            catch (Exception)
            {
                throw new Exception("Error Occured during Reading Information");
            }
            finally
            {
                ConnectionObj.Close();
            }
        }

        public string UpdateCatagory(int id,string Catagory)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText =string.Format("Update tblCatagories set EmpCatagory=@EmpCatagory where CatagoryID='" + id + "'");
                CommandObj.Parameters.Clear();
                CommandObj.Parameters.Add("@EmpCatagory", SqlDbType.VarChar).Value = Catagory;
                CommandObj.ExecuteNonQuery();
                return "Catagory Updated Successfully";
            }
            catch (Exception)
            {
                throw new Exception("Error Occured during Updating, Please try again");
            }
            finally
            {
                ConnectionObj.Close();
            }
        }

        public string Delete(int id)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = string.Format("Delete from tblCatagories where CatagoryID='" + id + "'");
                CommandObj.ExecuteNonQuery();
                return "Catagory Deleted Successfully";
            }
            catch (Exception)
            {
                throw new Exception("Error Occured during Deleting, Please try again");
            }
            finally
            {
                ConnectionObj.Close();
            }
        }
    }
}